var ajax = function (url, method, data, isAsync) {
  return new Promise((resolve, reject) => {
    var xhr = new XMLHttpRequest()
    xhr.open(method||'get', url, isAsync||true)
    // xhr.responseType='json'
    // xhr.setRequestHeader('Content-type','applicaiton/x-www-form-urlencodeed,chartset=utf-8')
    xhr.withCredentials=true
    xhr.send(data)
    xhr.onload = function () {//请求成功完成时触发。
      switch (xhr.status) {
        case 200: resolve(xhr.response); break;
        case 204: resolve(); break;
        case 401:// 401登录失败处理
          //do something
          reject();
          break;
        default: reject({ status: xhr.status, res: xhr.response });
      }
    }
    /**
     * readyState 的状态改变的时候，callback 函数会被调用。
     * xhr.onreadystatechange=function () {
        if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
          console.log(xhr.responseText)
          resolve(xhr.responseText)
        }
        if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 204) {
          resolve(xhr.status);
        }
        if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 401) {
          console.log(xhr.responseText)
          reject(xhr.status);
        }
     }
     * 
     * 
     * 
     */
  })
}
module.exports=ajax